package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.StoreLockMapper;
import com.zhijian.warehouse.manage.entity.po.StoreLock;
import com.zhijian.warehouse.manage.entity.vo.request.StoreLockSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreLockSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreLockVo;
import com.zhijian.warehouse.manage.service.StoreLockService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 锁盘表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-27 13:43:23
 */
@Slf4j
@Service
public class StoreLockServiceImpl implements StoreLockService {

    @Autowired
    private StoreLockMapper mapper;

    @Override
    public StoreLock selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreLockVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreLock storeLock) {
        Date now = new Date();
        storeLock.setCreateTime(now);
        storeLock.setUpdateTime(now);
        return mapper.insert(storeLock) > 0 ? storeLock.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreLock storeLock) {
        Date now = new Date();
        storeLock.setCreateTime(now);
        storeLock.setUpdateTime(now);
        return mapper.insertSelective(storeLock) > 0 ? storeLock.getId() : 0L;
    }

    @Override
    public boolean update(StoreLock storeLock) {
        Date now = new Date();
        storeLock.setUpdateTime(now);
        return mapper.updateByPrimaryKey(storeLock) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreLock record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreLockSaveOrUpdateVo saveOrUpdateVo) {
        StoreLock storeLock = new StoreLock();
        BeanUtils.copyProperties(saveOrUpdateVo, storeLock);
        return this.insert(storeLock) != 0;
    }

    @Override
    public boolean modify(StoreLockSaveOrUpdateVo saveOrUpdateVo) {
        StoreLock old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreLock storeLock = new StoreLock();
        BeanUtils.copyProperties(saveOrUpdateVo, storeLock);
        return this.update(storeLock);
    }

    @Override
    public StoreLockVo selectOne(StoreLockSearchVo storeLockSearchVo) {
        return mapper.selectOne(storeLockSearchVo);
    }

    @Override
    public Integer count(StoreLockSearchVo storeLockSearchVo) {
        return mapper.count(storeLockSearchVo);
    }

    @Override
    public List<StoreLockVo> selectList(StoreLockSearchVo storeLockSearchVo) {
        return mapper.selectList(storeLockSearchVo);
    }

    @Override
    public Page<StoreLockVo> selectPage(StoreLockSearchVo storeLockSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeLockSearchVo.getPageNum()) || Objects.isNull(storeLockSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeLockSearchVo);
        //分页
        Page<StoreLockVo> page = new Page<>(true, count, storeLockSearchVo.getPageNum(), storeLockSearchVo.getPageSize());
        storeLockSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreLockVo> responseVos = this.selectList(storeLockSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreLock> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(StoreLockSearchVo storeLockSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(storeLockSearchVo);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public StoreLockVo selectByWarehouseId(Long warehouseId) {
        StoreLockVo storeLock = mapper.selectByWarehouseId(warehouseId);
        if (BeanUtil.isEmpty(storeLock)) {
            insertSelective(StoreLock.builder().type(0).createUserId(0L).updateUserId(0L).warehouseId(warehouseId).build());
            return selectByWarehouseId(warehouseId);
        }
        return storeLock;
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public boolean selectLock(Long warehouseId) {
        AssertCommonUtil.notNull(warehouseId, "仓库id不能为空");
        StoreLockVo storeLock = selectOne(StoreLockSearchVo.builder()
                .warehouseId(warehouseId)
                .build());
        return BeanUtil.isNotEmpty(storeLock) && storeLock.getType() == 1;
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public boolean updateLock(StoreLockSaveOrUpdateVo param) {
        StoreLockVo storeLock = selectByWarehouseId(param.getWarehouseId());
        if (BeanUtil.isEmpty(storeLock))
            return insert(StoreLock.builder().type(param.getType()).warehouseId(param.getWarehouseId()).build()) > 0;
        return updateByPrimaryKeySelective(StoreLock.builder().id(storeLock.getId()).type(param.getType()).build());
    }
}
